package com.huawei.esdk.tp.northcommu.cxf.common.convert;

import java.util.ArrayList;
import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.tp.common.CipherUtils;
import com.huawei.esdk.tp.common.EnumRangeUtils;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.cxf.enumgen.ConfStatusMapping;
import com.huawei.esdk.tp.cxf.enumgen.DialModeMapping;
import com.huawei.esdk.tp.cxf.enumgen.PresentationTypeMapping;
import com.huawei.esdk.tp.cxf.enumgen.SiteFromMapping;
import com.huawei.esdk.tp.domain.model.Conference;
import com.huawei.esdk.tp.domain.model.ScheduleConference;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.AddSiteToScheduledConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ConfSitesStatusMap;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ConferenceInfo;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.EditScheduledConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.EditScheduledConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryConfSitesStatusResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryConferenceStatusResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleDynamicSiteConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleDynamicSiteConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleImmediateConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleImmediateConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.SiteInfo;


/** * @author w00208247 * * */
public class TPCommonConfMgrConvert
{
    
    /**
     * 预约会议，预约带匿名会场的会议，修改会议
     * 三个方法传入的参数除了类型不一致以外，里面的内容完全一致
     * 将三个bean的赋值合并
     * 北向soapbean转北向领域bean，用于赋值的情况
     * @throws SDKException 
     */
    public ScheduleConference scheduleConfCommSoap2model(ScheduleConf parameters) throws SDKException{
        //通过领域模型将行业相关bean转到产品无关bean-----------传入，返回ScheduleConference是否可行...?
        ScheduleConference conference = new ScheduleConference();
        String confName = parameters.getScheduleConf().getName();
        XMLGregorianCalendar beginTime = parameters.getScheduleConf().getBeginTime();
        Integer duration =parameters.getScheduleConf().getDuration();
        String rate = parameters.getScheduleConf().getRate();
        //预约会议，预约静态会议的时候confId是0
        String confId = parameters.getScheduleConf().getConfId();
        //预约会议的时候密码限定为6位，密码对会议没有作用
        String password = parameters.getScheduleConf().getPassword();
        conference.setId(confId);
        
        List<SiteInConference> siteInConferences = new ArrayList<SiteInConference>();
        SiteInConference site = null;
        conference.setName(confName);
        conference.setBeginTime(DateUtils.toDate(beginTime));
        conference.setLength(duration);
        conference.setRate(rate);
        
        //R3修改
        if(!StringUtils.isEmpty(password))
        {
            String encryptedPwd = CipherUtils.decodeFromBase64(password);
            if(null != encryptedPwd && !encryptedPwd.isEmpty())
            {
                conference.setPassword(encryptedPwd);
            }
            else
            {
                SDKException sdkException = new SDKException(ErrInfo.SDK_TP_PASSWORD_DECODE_ERRORDESC);
                sdkException.setSdkErrCode(ErrInfo.SDK_TP_PASSWORD_DECODE_ERRORCODE);
                throw sdkException;
            }
        }
        else
        {
            conference.setPassword(password);
        }
        
        /*
         * fix DTS2013091604313
         * common中未对部分输入参数转换到领域模型中
         */
        conference.setAccessCode(parameters.getScheduleConf().getAccessCode());
        conference.setCpResouce(parameters.getScheduleConf().getCpResouce());
        //枚举值判断
        EnumRangeUtils.enumRange(parameters.getScheduleConf().getPresentation(),
            PresentationTypeMapping.class);
        conference.setPresentation(parameters.getScheduleConf().getPresentation());
        
        List<SiteInfo> siteInfos = parameters.getScheduleConf().getSites();
        for (SiteInfo siteInfo : siteInfos)
        {
            site =  new SiteInConference(confId,siteInfo.getUri());
            if (null != SiteFromMapping.int2Enum(siteInfo.getFrom()))
            {
                site.setFrom(SiteFromMapping.int2Enum(siteInfo.getFrom()).toString());
            }
            if (null != DialModeMapping.int2Enum(siteInfo.getDialingMode()))
            {
                site.setDialingMode(DialModeMapping.int2Enum(siteInfo.getDialingMode()).toString());
            }
            
            //R3修改
            site.setRate(siteInfo.getRate());

            site.setVideoFormat(siteInfo.getVideoFormat());

            
            siteInConferences.add(site);
        }
        
        //R3新增
        if(null != parameters.getScheduleConf().isIsRecording())
        {
            conference.setIsRecording(parameters.getScheduleConf().isIsRecording() ? 1 : 0);
       
        }
        if(null != parameters.getScheduleConf().isIsLiveBroadcast())
        {
            conference.setIsLiveBroadcast(parameters.getScheduleConf().isIsLiveBroadcast() ? 1 : 0);
        }

        //R3修改
        String chairmanPwd = parameters.getScheduleConf().getChairmanPassword();
        if(!StringUtils.isEmpty(chairmanPwd))
        {
            String encryptedPwd = CipherUtils.decodeFromBase64(chairmanPwd);
            if(null != encryptedPwd && !encryptedPwd.isEmpty())
            {
                conference.setChairmanPassword(encryptedPwd);
            }
            else
            {
                SDKException sdkException = new SDKException(ErrInfo.SDK_TP_PASSWORD_DECODE_ERRORDESC);
                sdkException.setSdkErrCode(ErrInfo.SDK_TP_PASSWORD_DECODE_ERRORCODE);
                throw sdkException;
            }
        }
        else
        {
            conference.setChairmanPassword(chairmanPwd);
        }

        conference.setAuxVideoFormat(parameters.getScheduleConf().getAuxVideoFormat());

        conference.setSites(siteInConferences);
        
        return conference;
    }
    
    /**
     * 预约会议，预约带匿名会场的会议，修改会议
     * 三个方法传入的参数除了类型不一致以外，里面的内容完全一致
     * 将三个bean的赋值合并
     * 北向领域bean转北向soap bean，用于返回的情况
     */
    public ScheduleConfResponse scheduleConfCommModel2soap(SDKResult<ScheduleConference> result){
        ScheduleConfResponse scheduleConfResponse = new ScheduleConfResponse();
        
        int errorCode = (int)result.getErrCode();
        scheduleConfResponse.setResultCode(errorCode);
        //从返回值中取出返回消息，赋给北向soapbean
        
        //首先判断返回值是否为0，如果不为0，直接返回错误码
        if(errorCode!=0){
            return scheduleConfResponse;
        }
        ConferenceInfo conferenceInfo = new ConferenceInfo();
        conferenceInfo.setConfId( result.getResult().getId()+"");
        
        List<SiteInConference> siteInConferences = result.getResult().getSites();
        SiteInfo siteInfo = null;
        for (SiteInConference siteInConference : siteInConferences)
        {
            siteInfo = new SiteInfo();
            siteInfo.setName(siteInConference.getName());
            siteInfo.setUri(siteInConference.getUri());
            
            conferenceInfo.getSites().add(siteInfo);
            //返回多个accessCode，考虑多MCU的情况
//            siteInfo.setAccessCode(siteInConference.getAccessCode());
            conferenceInfo.setAccessCode(siteInConference.getAccessCode());
        }
        
        scheduleConfResponse.setConferenceInfo(conferenceInfo);
        
        return scheduleConfResponse;
    }
    
    /**
     * 预约会议，北向soapbean转北向领域bean
     * 用于赋值的情况
     *  ModelBean soap2model(SoapBean){}
     * @throws SDKException 
     */
    public ScheduleConference scheduleConfSoap2model(ScheduleConf parameters) throws SDKException
    {
        return scheduleConfCommSoap2model(parameters);
    }
    
    /**
     * 预约会议，北向领域bean转北向soap bean
     * 用于返回的情况
     * response model2soap
     */
    public ScheduleConfResponse scheduleConferenceModel2soap(SDKResult<ScheduleConference> result){
        return scheduleConfCommModel2soap(result);
    }
    /**
     * 预约即时会议，北向soapbean转北向领域bean
     * 用于赋值的情况
     *  ModelBean soap2model(SoapBean){}
     * @throws SDKException 
     */
    public ScheduleConference scheduleImmediateConfSoap2model(ScheduleImmediateConf parameters) throws SDKException
    {
        ScheduleConf scheduleConf = new ScheduleConf();
        scheduleConf.setScheduleConf(parameters.getScheduleConf());
        return scheduleConfCommSoap2model(scheduleConf);
    }
    /**
     * 预约即时会议，北向领域bean转北向soap bean
     * 用于返回的情况
     * response model2soap
     */
    public ScheduleImmediateConfResponse scheduleImmediateConferenceModel2soap(SDKResult<ScheduleConference> result){
        ScheduleConfResponse scheduleConfResponse = scheduleConfCommModel2soap(result);
        ScheduleImmediateConfResponse scheduleImmediateConfResponse = new ScheduleImmediateConfResponse();
        scheduleImmediateConfResponse.setConferenceInfo(scheduleConfResponse.getConferenceInfo());
        scheduleImmediateConfResponse.setResultCode(scheduleConfResponse.getResultCode());
        
        return scheduleImmediateConfResponse;
    }
    
    /**
     * 预约带匿名会场的会议，北向soapbean转北向领域bean
     * 用于赋值的情况
     *  ModelBean soap2model(SoapBean){}
     * @throws SDKException 
     */
    public ScheduleConference scheduleDynamicConfSoap2model(ScheduleDynamicSiteConf parameters) throws SDKException
    {
        ScheduleConf scheduleConf = new ScheduleConf();
        scheduleConf.setScheduleConf(parameters.getScheduleConf());
        return scheduleConfCommSoap2model(scheduleConf);
    }
    /**
     * 预约带匿名会场的会议，北向领域bean转北向soap bean
     * 用于返回的情况
     * response model2soap
     */
    public ScheduleDynamicSiteConfResponse scheduleDynamicConferenceModel2soap(SDKResult<ScheduleConference> result){
        
        ScheduleDynamicSiteConfResponse scheduleDynamicSiteConfResponse = new ScheduleDynamicSiteConfResponse();
        ScheduleConfResponse scheduleConfResponse = scheduleConfCommModel2soap(result);
        scheduleDynamicSiteConfResponse.setConferenceInfo(scheduleConfResponse.getConferenceInfo());
        scheduleDynamicSiteConfResponse.setResultCode(scheduleConfResponse.getResultCode());
        
        return scheduleDynamicSiteConfResponse;
    }

    /**
     * 修改会议，北向soapbean转北向领域bean
     * 用于赋值的情况
     *  ModelBean soap2model(SoapBean){}
     * @throws SDKException 
     */
    public ScheduleConference editScheduledConfSoap2model(EditScheduledConf parameters) throws SDKException
    {
        ScheduleConf scheduleConf = new ScheduleConf();
        scheduleConf.setScheduleConf(parameters.getEditConf());
        
        return scheduleConfCommSoap2model(scheduleConf);
    }
    
    /**
     * 预约带匿名会场的会议，北向领域bean转北向soap bean
     * 用于返回的情况
     * response model2soap
     */
    public EditScheduledConfResponse editScheduledConfModel2soap(SDKResult<ScheduleConference> result)
    {
        EditScheduledConfResponse editScheduleConfResponse = new EditScheduledConfResponse();
        ScheduleConfResponse scheduleConfResponse = scheduleConfCommModel2soap(result);
        
        editScheduleConfResponse.setConferenceInfo(scheduleConfResponse.getConferenceInfo());
        editScheduleConfResponse.setResultCode(scheduleConfResponse.getResultCode());
       return editScheduleConfResponse;
    }
    
    /**
     * 查询会议会场状态，北向领域bean转北向soap bean
     * 用于返回的情况
     * response model2soap
     */
    public QueryConfSitesStatusResponse queryConfSiteStatusModel2soap(SDKResult<List<SiteInConference>> result)
    {
        QueryConfSitesStatusResponse queryConfSitesStatusResponse = new QueryConfSitesStatusResponse();
        //将领域bean中的值赋给协议相关的bean
        int resultCode = (int)result.getErrCode();
        if (resultCode == 0)
        {
            ConfSitesStatusMap confSiteStatusMap = null;
            List<SiteInConference> sitesInConference = result.getResult();
            for (int j = 0; j < sitesInConference.size(); j++)
            {
                confSiteStatusMap = new ConfSitesStatusMap();
                confSiteStatusMap.setSiteUri(sitesInConference.get(j).getUri());
                confSiteStatusMap.setConfSiteStatus(sitesInConference.get(j).getStatus());
                
                queryConfSitesStatusResponse.getConfSitesStatus().add(confSiteStatusMap);
            }
        }
        queryConfSitesStatusResponse.setResultCode(resultCode);
        
        return queryConfSitesStatusResponse;
    }

    public AddSiteToScheduledConfResponse addSiteToScheduledConfModel2NorthSoap(SDKResult<String> result)
    {
        AddSiteToScheduledConfResponse commonResponse = new AddSiteToScheduledConfResponse();
        commonResponse.setResultCode((int)result.getErrCode());
        commonResponse.setAccessCode(result.getResult());
        
        return commonResponse;
    }

    public QueryConferenceStatusResponse queryConferencesStatusModel2soap(
            SDKResult<Conference> result)
    {
        QueryConferenceStatusResponse response = new QueryConferenceStatusResponse();
        response.setResultCode((int)result.getErrCode());
        if (null != result.getResult())
        {
            response.setConferenceStatus(ConfStatusMapping.str2Int(result.getResult().getConferenceStatus().getStatus()));
        }
        return response;
    }

}
